<?php
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition End User License Agreement
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magento.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    design
 * @package     default_default
 * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license http://www.magento.com/license/enterprise-edition
 */
?>
<?php $_htmlId      = $this->getElement()->getHtmlId() ?>
<?php $_htmlClass   = $this->getElement()->getClass() ?>
<?php $_htmlName    = $this->getElement()->getName() ?>
<?php $_readonly    = $this->getElement()->getReadonly() ?>
<?php $_multiWebsite= $this->isMultiWebsites(); ?>
<tr>
    <td class="label"><?php echo $this->getElement()->getLabel() ?></td>
    <td colspan="10" class="grid giftcard-amounts">
    <table cellspacing="0" class="data border" id="<?php echo $_htmlId; ?>_table">
        <?php if ($_multiWebsite): ?>
        <col width="135" />
        <?php endif; ?>
        <col />
        <col width="1" />
        <thead>
            <tr class="headings">
                <th <?php if (!$_multiWebsite): ?>style="display:none"<?php endif; ?>><?php echo Mage::helper('sales')->__('Website') ?></th>
                <th><?php echo Mage::helper('catalog')->__('Amount') ?> <span class="required">*</span></th>
                <th class="last"><?php echo Mage::helper('catalog')->__('Action') ?></th>
            </tr>
            <tr id="<?php echo $_htmlId ?>_add_template" class="template no-display">
                <td <?php if (!$_multiWebsite): ?>style="display:none"<?php endif; ?>>
                <select disabled="no-template" class="<?php echo $_htmlClass ?> required-entry" name="<?php echo $_htmlName ?>[__index__][website_id]" id="#{prefix}_giftcard_amounts_row___index___website">
                    <?php foreach ($this->getWebsites() as $_websiteId => $_info): ?>
                    <option value="<?php echo $_websiteId ?>"><?php echo $_info['name'] ?><?php if (!empty($_info['currency'])): ?> [<?php echo $_info['currency'] ?>]<?php endif; ?></option>
                    <?php endforeach ?>
                </select>
                </td>
                <td>
                    <input disabled="no-template" class="<?php echo $_htmlClass ?> required-entry validate-greater-than-zero" type="text" name="<?php echo $_htmlName ?>[__index__][price]" value="'#{price}'" />
                </td>
                <td class="last">
                    <input type="hidden" name="<?php echo $_htmlName ?>[__index__][delete]" class="delete" disabled="no-template" value=""  id="#{prefix}_giftcard_amounts_row___index___delete" />
                    <button title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('enterprise_giftcard')->__('Delete Amount')); ?>" class="scalable delete icon-btn delete-product-option" onclick="giftcardAmountsControl.deleteItem('<?php echo $_htmlId ?>', event);return false"><span><span><span><?php echo Mage::helper('enterprise_giftcard')->__('Delete'); ?></span></span></span></button>
                </td>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td <?php if (!$_multiWebsite): ?>style="display:none"<?php endif; ?>></td>
                <td colspan="2" class="a-right"><?php echo $this->getAddButtonHtml() ?></td>
            </tr>
        </tfoot>
        <tbody id="<?php echo $_htmlId ?>_container">
        </tbody>
    </table>
    <input type="hidden" id="giftcard_amounts_total" name="giftcard_amounts_total" value="" <?php if (!$_readonly): ?>class="required-entry"<?php endif; ?>>

<script type="text/javascript">
//<![CDATA[
    if (typeof itemsCount == 'undefined') {
        var itemsCount = 0;
    }
    var giftcardAmountsControl = {
        deleteButton: false,
        addItem : function () {
            var data = {};
            data.prefix = '';
            data.website_id = 0;
            data.price      = '';
            data.index      = itemsCount++;
            if(arguments.length == 3) {
                data.prefix     = arguments[0];
                data.website_id = arguments[1];
                data.price      = arguments[2];
            } else if (arguments.length == 1) {
                data.prefix     = arguments[0];
            }

            var template = new Template('<tr>' + $(data.prefix + '_add_template').innerHTML.replace(/__index__/g, '#{index}').replace(/ disabled="?no-template"?/g, '').replace(/ disabled/g, '').replace(/="'([^']*)'"/g, '="$1"') + '</tr>');
            Element.insert($(data.prefix + '_container'), {'bottom':template.evaluate(data)});

            $(data.prefix + '_giftcard_amounts_row_'+data.index+'_website').value = data.website_id;
            <?php if ($_readonly): ?>
            $('<?php echo $_htmlId ?>_container').select('input', 'select')
                .each(this.disableElement);
            $('<?php echo $_htmlId ?>_container').select('button')
                .each(this.disableElement);
            <?php endif; ?>
            this.updateTotalAmounts();
        },
        disableElement: function(elem) {
            elem.disabled = true;
            $(elem).addClassName('disabled');
        },

        deleteItem : function(prefix, event) {
            var tr = Event.findElement(event, 'tr');
            if (tr) {
                Element.select(tr, '.delete').each(function(elem){elem.value='1'});
                Element.select(tr, ['input', 'select']).each(function(elem){elem.hide()});
                Element.hide(tr);
                Element.addClassName(tr, 'no-display template');
            }
            itemsCount--;
            this.updateTotalAmounts();
        },

        updateTotalAmounts: function() {
            $('giftcard_amounts_total').value = (itemsCount > 0 ? itemsCount : '');
        }
    };

    <?php foreach ($this->getValues() as $_item): ?>
    giftcardAmountsControl.addItem('<?php echo $_htmlId ?>', '<?php echo $_item['website_id'] ?>', '<?php echo sprintf('%.2f', $_item['value']) ?>');
    <?php endforeach; ?>

    function updatePriceAmountValidation() {
        if($('allow_open_amount')) {
            if ($('allow_open_amount').value == 1) {
                productForm.validator.reset('giftcard_amounts_total');
                Element.removeClassName($('giftcard_amounts_total'), 'required-entry');
            } else {
                if (!Element.hasClassName($('giftcard_amounts_total'), 'required-entry')) {
                    Element.addClassName($('giftcard_amounts_total'), 'required-entry');
                }
            }
        }
    }

    function setupPriceAmountEvents() {
        if ($('allow_open_amount')) {
            Event.observe('allow_open_amount', 'change', updatePriceAmountValidation);
        }
        updatePriceAmountValidation();
    }
    <?php if (!$_readonly): ?>
    Event.observe(window, 'load', setupPriceAmountEvents);
    <?php endif; ?>
//]]>
</script>
</td>
</tr>
